package com.weijin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijin.mapper.ChatHistoryMapper;
import com.weijin.model.ChatHistory;
import com.weijin.service.IChatHistoryService;
import com.weijin.vo.PageVo;
import org.springframework.stereotype.Service;
import com.weijin.utlis.DateTimeUtil;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/20 15:12
 */
@Service
public class ChatHistoryServiceImpl implements IChatHistoryService {

    @Resource
    private ChatHistoryMapper chatHistoryMapper;

    @Override
    public ChatHistory addChatHistory(ChatHistory chatHistory) {
        if (Objects.isNull(chatHistory)) {
            throw new RuntimeException("聊天记录不能为空");
        }
        chatHistory.setCreatedAt(DateTimeUtil.getDateTime());
        int inserted = chatHistoryMapper.insert(chatHistory);
        if (inserted > 0) {
            return chatHistory;
        }
        throw new RuntimeException("添加聊天记录失败");
    }

    @Override
    public PageVo<ChatHistory> pageChatHistory(Integer pageNumber, Integer pageSize) {
        LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<ChatHistory>().orderByAsc(ChatHistory::getCreatedAt);
        IPage<ChatHistory> page = new Page<>(pageNumber, pageSize);
        IPage<ChatHistory> dataPage = chatHistoryMapper.selectPage(page, wrapper);
        if (Objects.isNull(dataPage)) {
            throw new RuntimeException("查询历史记录失败");
        }
        return new PageVo<>(dataPage);
    }
}
